home *** CD-ROM | disk | FTP | other *** search
/ Turnbull China Bikeride / Turnbull China Bikeride - Disc 2.iso / STUTTGART / TEX-UTIL / DVIPS_55 / dvips / src / c / virtualfon < prev   
Text File  |  1994-05-06  |  7KB  |  276 lines

  1. /*
  2.  *   Here's the code to load a VF file into memory.
  3.  *   Any resemblance between this file and loadfont.c is purely uncoincidental.
  4.  */
  5. #include "dvips.h" /* The copyright notice in that file is included too! */
  6. /*
  7.  *   These are the external routines we use.
  8.  */
  9. extern void makefont() ;
  10. extern void error() ;
  11. extern integer scalewidth() ;
  12. extern FILE *search() ;
  13. extern fontdesctype *newfontdesc() ;
  14. extern fontdesctype *matchfont() ;
  15. /*
  16.  *   These are the external variables we use.
  17.  */
  18. #ifdef DEBUG
  19. extern integer debug_flag;
  20. #endif  /* DEBUG */
  21. extern long bytesleft ;
  22. extern quarterword *raster ;
  23. /* extern char *vfpath ; */
  24. extern char errbuf[200] ;
  25. extern real conv ;
  26. extern real vconv ;
  27. extern real alpha ;
  28. extern integer mag ;
  29. extern int actualdpi ;
  30. extern int vactualdpi ;
  31. extern char *nextstring, *maxstring ;
  32. extern fontdesctype *fonthead ;
  33. extern real alpha ;
  34. /*
  35.  *   Now we have some routines to get stuff from the VF file.
  36.  *   Subroutine vfbyte returns the next byte.
  37.  */
  38. static FILE *vffile ;
  39. static char name[50] ;
  40. void
  41. badvf(s)
  42.    char *s ;
  43. {
  44.    (void)sprintf(errbuf,"! Bad VF file %s: %s",name,s) ;
  45.    error(errbuf);
  46. }
  47.  
  48. shalfword
  49. vfbyte()
  50. {
  51.    register shalfword i ;
  52.  
  53.    if ((i=getc(vffile))==EOF)
  54.       badvf("unexpected eof") ;
  55.    return(i) ;
  56. }
  57.  
  58. integer
  59. vfquad()
  60. {
  61.    register integer i ;
  62.  
  63.    i = vfbyte() ;
  64.    if (i > 127)
  65.       i -= 256 ;
  66.    i = i * 256 + vfbyte() ;
  67.    i = i * 256 + vfbyte() ;
  68.    i = i * 256 + vfbyte() ;
  69.    return(i) ;
  70. }
  71.  
  72. integer
  73. vftrio()
  74. {
  75.    register integer i ;
  76.  
  77.    i = vfbyte() ;
  78.    i = i * 256 + vfbyte() ;
  79.    i = i * 256 + vfbyte() ;
  80.    return(i) ;
  81. }
  82.  
  83. Boolean
  84. vfopen(fd)
  85.         register fontdesctype *fd ;
  86. {
  87.    register char *d, *n ;
  88.  
  89.    d = fd->area ;
  90.    n = fd->name ;
  91.    if (*d==0)
  92.       d = vfpath ;
  93. #ifdef MVSXA   /* IBM: MVS/XA */
  94.    (void)sprintf(name, "vf(%s)", n) ;
  95. #else
  96.    (void)sprintf(name, "%s.vf", n) ;
  97. #endif
  98.    if (0 != (vffile=search(d, name, READBIN)))
  99.       return(1) ;
  100.    return(0) ;
  101. }
  102.  
  103. /*
  104.  * The following routine is like fontdef, but for local fontdefs in VF files.
  105.  */
  106. fontmaptype *
  107. vfontdef(s, siz)
  108.       integer s ;
  109.     int siz ;
  110. {
  111.    register integer i, j, fn ;
  112.    register fontdesctype *fp ;
  113.    register fontmaptype *cfnt ;
  114.    char *name, *area ;
  115.    integer cksum, scsize, dssize ;
  116.  
  117.    fn = vfbyte() ;
  118.    while (siz-- > 1)
  119.       fn = (fn << 8) + vfbyte() ;
  120.    cfnt = (fontmaptype *)mymalloc((integer)sizeof(fontmaptype)) ;
  121.    cfnt->fontnum = fn ;
  122.    cksum = vfquad() ;
  123.    scsize = scalewidth(s, vfquad()) ;
  124.    dssize = (integer)(alpha * (real)vfquad()) ;
  125.    i = vfbyte() ; j = vfbyte() ;
  126.    if (nextstring + i + j > maxstring)
  127.       error("! out of string space") ;
  128.    area = nextstring ;
  129.    for (; i>0; i--)
  130.       *nextstring++ = vfbyte() ;
  131.    *nextstring++ = 0 ;
  132.    name = nextstring ;
  133.    for (; j>0; j--)
  134.       *nextstring++ = vfbyte() ;
  135.    *nextstring++ = 0 ;
  136.    fp = matchfont(name, area, scsize, (char *)0) ;
  137.    if (fp) {
  138.       nextstring = name ;
  139.       fp->checksum = cksum ;
  140.    } else {
  141.       fp = newfontdesc(cksum, scsize, dssize, name, area) ;
  142.       fp->next = fonthead ;
  143.       fonthead = fp ;
  144.    }
  145.    cfnt->desc = fp ;
  146.    return (cfnt) ;
  147. }
  148.  
  149. /*
  150.  *   Now our virtualfont routine.
  151.  */
  152. Boolean
  153. virtualfont(curfnt)
  154.         register fontdesctype *curfnt ;
  155. {
  156.    register shalfword i ;
  157.    register shalfword cmd ;
  158.    register integer k ;
  159.    register integer length ;
  160.    register shalfword cc ;
  161.    register chardesctype *cd ;
  162.    integer scaledsize = curfnt->scaledsize ;
  163.    register quarterword *tempr ;
  164.    fontmaptype *fm, *newf ;
  165.  
  166.    if (!vfopen(curfnt))
  167.       return (0) ;
  168. #ifdef DEBUG
  169.    if (dd(D_FONTS))
  170.       (void)fprintf(stderr,"Loading virtual font %s at %.1fpt\n",
  171.          name, (real)scaledsize/(alpha*0x100000)) ;
  172. #endif /* DEBUG */
  173.  
  174. /*
  175.  *   We clear out some pointers:
  176.  */
  177.    for (i=0; i<256; i++) {
  178.       curfnt->chardesc[i].TFMwidth = 0 ;
  179.       curfnt->chardesc[i].packptr = NULL ;
  180.       curfnt->chardesc[i].pixelwidth = 0 ;
  181.       curfnt->chardesc[i].flags = 0 ;
  182.    }
  183.    if (vfbyte()!=247)
  184.       badvf("expected pre") ;
  185.    if (vfbyte()!=202)
  186.       badvf("wrong id byte") ;
  187.    for(i=vfbyte(); i>0; i--)
  188.       (void)vfbyte() ;
  189.    k = vfquad() ;
  190.    if (k && curfnt->checksum)
  191.       if (k!=curfnt->checksum) {
  192.          (void)sprintf(errbuf,"Checksum mismatch in font %s", name) ;
  193.          error(errbuf) ;
  194.        }
  195.    k = (integer)(alpha * (real)vfquad()) ;
  196.    if (k > curfnt->designsize + 2 || k < curfnt->designsize - 2) {
  197.       (void)sprintf(errbuf,"Design size mismatch in font %s", name) ;
  198.       error(errbuf) ;
  199.    }
  200. /*
  201.  * Now we look for font definitions.
  202.  */
  203.    fm = NULL ;
  204.    while ((cmd=vfbyte())>=243) {
  205.       if (cmd>246)
  206.          badvf("unexpected command in preamble") ;
  207.       newf = vfontdef(scaledsize, cmd-242) ;
  208.       if (fm)
  209.          fm->next = newf ;
  210.       else curfnt->localfonts = newf ;
  211.       fm = newf ;
  212.       fm->next = NULL ; /* FIFO */
  213.    }
  214. /*
  215.  *   Now we get down to the serious business of reading character definitions.
  216.  */
  217.    do {
  218.       if (cmd==242) {
  219.          length = vfquad() + 2 ;
  220.          if (length<2) badvf("negative length packet") ;
  221.          if (length>65535) badvf("packet too long") ;
  222.          cc = vfquad() ;
  223.          if (cc<0 || cc>255) badvf("character code out of range") ;
  224.          cd = curfnt->chardesc + cc ;
  225.          cd->TFMwidth = scalewidth(vfquad(), scaledsize) ;
  226.       } else {
  227.          length = cmd + 2;
  228.          cc = vfbyte() ;
  229.          cd = curfnt->chardesc + cc ;
  230.          cd->TFMwidth = scalewidth(vftrio(), scaledsize) ;
  231.       }
  232.       if (cd->TFMwidth >= 0)
  233.          cd->pixelwidth = ((integer)(conv*cd->TFMwidth+0.5)) ;
  234.       else
  235.          cd->pixelwidth = -((integer)(conv*-cd->TFMwidth+0.5)) ;
  236.       cd->flags = EXISTS ;
  237.       if (bytesleft < length) {
  238. #ifdef DEBUG
  239.           if (dd(D_FONTS))
  240.              (void)fprintf(stderr,
  241. #ifdef SHORTINT
  242.                    "Allocating new raster memory (%ld req, %ld left)\n",
  243. #else
  244.                    "Allocating new raster memory (%d req, %ld left)\n",
  245. #endif
  246.                                 length, bytesleft) ;
  247. #endif /* DEBUG */
  248.           if (length > MINCHUNK) {
  249.              tempr = (quarterword *)mymalloc((integer)length) ;
  250.              bytesleft = 0 ;
  251.           } else {
  252.              raster = (quarterword *)mymalloc((integer)RASTERCHUNK) ;
  253.              tempr = raster ;
  254.              bytesleft = RASTERCHUNK - length ;
  255.              raster += length ;
  256.          }
  257.       } else {
  258.          tempr = raster ;
  259.          bytesleft -= length ;
  260.          raster += length ;
  261.       }
  262.       cd->packptr = tempr ;
  263.       length -= 2 ;
  264.       *tempr++ = length / 256 ;
  265.       *tempr++ = length % 256 ;
  266.          for (; length>0; length--)
  267.             *tempr++ = vfbyte() ;
  268.       cmd = vfbyte() ;
  269.    } while (cmd < 243) ;
  270.    if (cmd != 248)
  271.       badvf("missing postamble") ;
  272.    (void)fclose(vffile) ;
  273.    curfnt->loaded = 2 ;
  274.    return (1) ;
  275. }
  276.